package com.victor.controller;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.RequestBody;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Jingqulist;
import com.victor.entity.group.GroupTour;
import com.victor.entity.group.GroupTour_Signup;
import com.victor.entity.group.Lycompany;
import com.victor.entity.group.LycompanyPhoto;
import com.victor.service.GroupService;
import com.victor.service.JingquService;
import com.victor.utils.Msg;

/**
 * 团游管理
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/group")
public class GroupController {

	@Autowired
	private GroupService service;

	@Autowired
	private JingquService jingquservice;

	// 获取团游景区列表
	@RequestMapping("/getGroupJqlist")
	public Msg getGroupTour(String jqclassid, String starttime, String endtime, String search) {
		// 创建返回景区list
		List<Jingqulist> jingqulist = new ArrayList<Jingqulist>();

		// 查询团游景区列表
		List<GroupTour> grouptours = service.getGroupTour(jqclassid, starttime, endtime, search);
		if (grouptours != null && grouptours.size() > 0) {
			for (GroupTour groupTour : grouptours) {
				jingqulist.add(jingquservice.getJQxqByid(groupTour.getJqid()));
			}
		}

		return Msg.success().add("jingqulist", jingqulist);
	}

	// 通过景区id查询景区详情及团游信息
	@RequestMapping("/getGroupTourXQ")
	public Msg getGroupTourXQ(String jqid) {
		// 通过景区id查询景区详情
		Jingqulist jqxq = jingquservice.getJQxqByid(jqid);
		if (jqxq == null) {
			return Msg.fail("未查询到该景区信息");
		}
		// 通过景区id查询开设景区团游的公司
		List<GroupTour> groupTours = service.getLycompany(jqid);

		if (groupTours == null) {
			return Msg.fail("未查询到开发该景区的公司");
		}

		return Msg.success().add("jqxq", jqxq).add("groupTours", groupTours);
	}

	// 通过旅游公司id查询旅游公司详情
	@RequestMapping("/getLycompanyXQ")
	public Msg getLycompanyXQ(String lycid) {
		// 通过旅游公司id查询旅游公司详情
		Lycompany lycompany = service.getLycompanyXQ(lycid);
		if (lycompany == null) {
			return Msg.fail("未查询到该旅游公司信息");
		}
		// 通过旅游公司id查询该公司都开设了哪些景区
		List<GroupTour> groupTours = service.getGroupTourList(lycid);
		if (groupTours == null) {
			return Msg.fail("未查询到该旅游公司开设景区信息");
		}
		return Msg.success().add("lycompany", lycompany).add("groupTours", groupTours);
	}

	// 获取旅游公司列表
	@RequestMapping("/getLycompanyList")
	public Msg getLycompanyList() {
		List<Lycompany> lycompanies = service.getLycompanyList();
		return Msg.success().add("lycompanies", lycompanies);
	}

	// 批量删除旅游公司
	@RequestMapping("/batchDeleteLycompany")
	public Msg batchDeleteLycompany(int[] ids) {
		boolean delResult = service.batchDeleteLycompany(ids);
		return Msg.judge(delResult);
	}

	// 新建旅游公司
	@RequestMapping("/buildLycompany")
	public Msg buildLycompany(@RequestBody Lycompany lycompany) {
		if (lycompany == null) {
			return Msg.fail("参数错误");
		}
		int insResult = service.addLycompany(lycompany);
		if (insResult > 0) {
			if (lycompany.getLycompanyPhotos() != null && lycompany.getLycompanyPhotos().size() > 0) {
				for (LycompanyPhoto lycompanyPhoto : lycompany.getLycompanyPhotos()) {
					lycompanyPhoto.setLcid(String.valueOf(lycompany.getId()));
					service.addLycompanyPhoto(lycompanyPhoto);
				}
			}
			return Msg.success();
		}
		return Msg.fail();
	}

	// 批量导出旅游公司列表
	@RequestMapping("exportLycompany")
	public void exportLycompany(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Lycompany> list = service.getLycompanyList();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("旅游公司详情");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short) 0, (short) 10);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("旅游公司详情");

		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("公司名称");
		headrow.createCell(1).setCellValue("公司简介");
		headrow.createCell(2).setCellValue("视频简介");

		for (Lycompany lycompany : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			if (lycompany.getC_name() == null) {
				dataRow.createCell(0).setCellValue("");
			} else {
				dataRow.createCell(0).setCellValue(lycompany.getC_name());
			}
			if (lycompany.getC_introduce() == null) {
				dataRow.createCell(1).setCellValue("");
			} else {
				dataRow.createCell(1).setCellValue(lycompany.getC_introduce());
			}
			if (lycompany.getC_video() == null) {
				dataRow.createCell(2).setCellValue("");
			} else {
				dataRow.createCell(2).setCellValue(lycompany.getC_video());
			}
		}

		String filename = "旅游公司详情";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
	}
	/**
	 * 团游列表
	 * @param page
	 * @param row
	 * @param search
	 * @param start
	 * @param end
	 * @return
	 */
	@RequestMapping("/grouptour")
	public Msg findAllGroup(int page,int row,String search,String start,String  end) {
		PageInfo<GroupTour> groundTour = service.selAllGroundTour(page,row,search,start,end);
		return Msg.success().add("grouptour", groundTour);
		
	}
	/**
	 * 删除团游
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delgrouptou")
	public Msg delGroupTour(int[] ids) {
		if (ids == null || ids.length<=0) {
			return Msg.fail("入参失败");
		}
		boolean tour = service.delGroupTour(ids);
		return Msg.judge(tour);
		
	}
	/**
	 * 导出团游列表
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("exporttour")
	public void exporttour(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<GroupTour> list = service.getGroupALL();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("团游详情");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("团游详情");

		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("序号");
		headrow.createCell(1).setCellValue("团游名称");
		headrow.createCell(2).setCellValue("开始时间");
		headrow.createCell(3).setCellValue("结束时间");
		headrow.createCell(4).setCellValue("团游价格");
		headrow.createCell(5).setCellValue("团游景区简介");
		headrow.createCell(6).setCellValue("团游市");

		for (GroupTour tour : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			if (tour.getId() == null) {
				dataRow.createCell(0).setCellValue("");
			} else {
				dataRow.createCell(0).setCellValue(tour.getId());
			}
			if (tour.getT_name()== null) {
				dataRow.createCell(1).setCellValue("");
			} else {
				dataRow.createCell(1).setCellValue(tour.getT_name());
			}
			if (tour.getT_starttime() == null) {
				dataRow.createCell(2).setCellValue("");
			} else {
				dataRow.createCell(2).setCellValue(tour.getT_starttime());
			}
			
			if (tour.getT_endtime() == null) {
				dataRow.createCell(3).setCellValue("");
			} else {
				dataRow.createCell(3).setCellValue(tour.getT_endtime());
			}
			if (tour.getT_price() == null) {
				dataRow.createCell(4).setCellValue("");
			} else {
				dataRow.createCell(4).setCellValue(tour.getT_price());
			}
			if (tour.getT_introduce() == null) {
				dataRow.createCell(5).setCellValue("");
			} else {
				dataRow.createCell(5).setCellValue(tour.getT_introduce());
			}
			if (tour.getCity() == null) {
				dataRow.createCell(6).setCellValue("");
			} else {
				dataRow.createCell(6).setCellValue(tour.getCity());
			}
		}

		String filename = "团游详情";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
	}
	
	
	/**
	 * 导出单个团游列表
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("exporttourbyid")
	public void exporttourByid(HttpServletRequest request, HttpServletResponse response,Integer id) throws Exception {
		GroupTour tour = service.getGroupALLById(id);
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("团游详情");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("团游详情");

		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("序号");
		headrow.createCell(1).setCellValue("团游名称");
		headrow.createCell(2).setCellValue("开始时间");
		headrow.createCell(3).setCellValue("结束时间");
		headrow.createCell(4).setCellValue("团游价格");
		headrow.createCell(5).setCellValue("团游景区简介");
		headrow.createCell(6).setCellValue("团游市");

		if (tour != null) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			if (tour.getId() == null) {
				dataRow.createCell(0).setCellValue("");
			} else {
				dataRow.createCell(0).setCellValue(tour.getId());
			}
			if (tour.getT_name()== null) {
				dataRow.createCell(1).setCellValue("");
			} else {
				dataRow.createCell(1).setCellValue(tour.getT_name());
			}
			if (tour.getT_starttime() == null) {
				dataRow.createCell(2).setCellValue("");
			} else {
				dataRow.createCell(2).setCellValue(tour.getT_starttime());
			}
			
			if (tour.getT_endtime() == null) {
				dataRow.createCell(3).setCellValue("");
			} else {
				dataRow.createCell(3).setCellValue(tour.getT_endtime());
			}
			if (tour.getT_price() == null) {
				dataRow.createCell(4).setCellValue("");
			} else {
				dataRow.createCell(4).setCellValue(tour.getT_price());
			}
			if (tour.getT_introduce() == null) {
				dataRow.createCell(5).setCellValue("");
			} else {
				dataRow.createCell(5).setCellValue(tour.getT_introduce());
			}
			if (tour.getCity() == null) {
				dataRow.createCell(6).setCellValue("");
			} else {
				dataRow.createCell(6).setCellValue(tour.getCity());
			}
		}

		String filename = "团游详情";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
	}

	/**
	 * 团游报名列表
	 * @param page
	 * @param row
	 * @param start
	 * @param end
	 * @param search
	 * @return
	 */
	@RequestMapping("/groupname")
	public Msg groupBaoming(int page,int row,String start,String end,String search,String tourid) {
		if (tourid == null || tourid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<GroupTour_Signup> selAllGround = service.selAllGround(page,row,start,end,search,tourid);
		return Msg.success().add("selAllGround", selAllGround);
	}
	/**
	 * 批量删除 团游报名
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delgroup")
	public Msg delGroup(int[] ids) {
		if (ids == null || ids.length <=0) {
			return Msg.fail("入参失败");
		}
		boolean b = service.delGroup(ids);
		return Msg.judge(b);
		
	}
	/**
	 * 导出团游报名
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/exportgroup")
	public void exportGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<GroupTour_Signup> list = service.selALL();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("团游报名详情");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short) 0, (short) 5);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("团游报名详情");

		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("序号");
		headrow.createCell(1).setCellValue("团游名称");
		headrow.createCell(2).setCellValue("参加时间");
		headrow.createCell(3).setCellValue("参团人姓名");
		headrow.createCell(4).setCellValue("参团人电话");
		headrow.createCell(5).setCellValue("参团人数");

		for (GroupTour_Signup gTour : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			if (gTour.getId() == null) {
				dataRow.createCell(0).setCellValue("");
			} else {
				dataRow.createCell(0).setCellValue(gTour.getId());
			}
			if (gTour.getGroupTour().getT_name()== null) {
				dataRow.createCell(1).setCellValue("");
			} else {
				dataRow.createCell(1).setCellValue(gTour.getGroupTour().getT_name());
			}
			if (gTour.getJointime()== null) {
				dataRow.createCell(2).setCellValue("");
			} else {
				dataRow.createCell(2).setCellValue(gTour.getJointime());
			}
			
			if (gTour.getUname() == null) {
				dataRow.createCell(3).setCellValue("");
			} else {
				dataRow.createCell(3).setCellValue(gTour.getUname());
			}
			if (gTour.getUphone() == null) {
				dataRow.createCell(4).setCellValue("");
			} else {
				dataRow.createCell(4).setCellValue(gTour.getUphone());
			}
			if (gTour.getPersonnum() == null) {
				dataRow.createCell(5).setCellValue("");
			} else {
				dataRow.createCell(5).setCellValue(gTour.getPersonnum());
			}
		}

		String filename = "团游报名详情";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
	}
	/**
	 * 导出名单
	 * @param request
	 * @param response
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping("/exportsinglegroup")
	public void exportSingleGroup(HttpServletRequest request, HttpServletResponse response,Integer id) throws Exception {
		GroupTour_Signup gTour = service.selGroundById(id);
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("团游报名");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("团游报名");

		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("序号");
		headrow.createCell(1).setCellValue("开始时间");
		headrow.createCell(2).setCellValue("结束时间");
		headrow.createCell(3).setCellValue("团游名称");
		headrow.createCell(4).setCellValue("团游价格");
		headrow.createCell(5).setCellValue("人数");

		if (gTour != null) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			if (gTour.getId() == null) {
				dataRow.createCell(0).setCellValue("");
			} else {
				dataRow.createCell(0).setCellValue(gTour.getId());
			}
			if (gTour.getGroupTour().getT_name()== null) {
				dataRow.createCell(1).setCellValue("");
			} else {
				dataRow.createCell(1).setCellValue(gTour.getGroupTour().getT_name());
			}
			if (gTour.getJointime()== null) {
				dataRow.createCell(2).setCellValue("");
			} else {
				dataRow.createCell(2).setCellValue(gTour.getJointime());
			}
			
			if (gTour.getUname() == null) {
				dataRow.createCell(3).setCellValue("");
			} else {
				dataRow.createCell(3).setCellValue(gTour.getUname());
			}
			if (gTour.getUphone() == null) {
				dataRow.createCell(4).setCellValue("");
			} else {
				dataRow.createCell(4).setCellValue(gTour.getUphone());
			}
			if (gTour.getPersonnum() == null) {
				dataRow.createCell(5).setCellValue("");
			} else {
				dataRow.createCell(5).setCellValue(gTour.getPersonnum());
			}
		}
		

		String filename = "团游报名";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
	}
	
	
}
